package org.dllwh.utils.apache.poi.word;


import cn.hutool.core.convert.Convert;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.dllwh.utils.apache.poi.word.model.BarChartForm;
import org.dllwh.utils.apache.poi.word.model.LineChartForm;
import org.dllwh.utils.apache.poi.word.model.PieChartForm;
import org.junit.Test;

import java.io.FileOutputStream;

import static org.dllwh.utils.apache.poi.word.WordCatalogue.buildTocPage;
import static org.dllwh.utils.apache.poi.word.WordCatalogue.createChapterTitle;
import static org.dllwh.utils.apache.poi.word.WordChart.*;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName WordHelperTest
 * @Description Java 操作 Apache POI Word 测试类
 * @Date 2025-11-20 10:07
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class WordHelperTest {
    private static final String FILE_PATH = "C:\\Users\\admin\\Downloads\\";

    public static void main(String[] args) {
        try (XWPFDocument document = WordDocument.createDocument()) {
            WordDocument.setPageMargin(document, Convert.toLong(Units.MASTER_DPI * 3.6), Convert.toLong(Units.MASTER_DPI * 3.6), Convert.toLong(Units.MASTER_DPI * 2.7), Convert.toLong(Units.MASTER_DPI * 2.7));
            WordDocument.saveDocument(document, FILE_PATH + "CreateWordWithTOC.docx");
        } catch (Exception e) {
            throw new RuntimeException("创建word文档对象失败！", e);
        }
    }

    @Test
    public void createWordWithTocTest() {
        try (XWPFDocument document = WordDocument.createDocument()) {
            // 添加目录页
            buildTocPage(document);

            // 添加章节标题（带样式以便生成目录）
            createChapterTitle(document, "第一章：引言", 1);
            createChapterTitle(document, "1.1 背景介绍", 2);
            // 添加内容
            XWPFParagraph content = document.createParagraph();
            // 设置首行缩进 2 字符
            content.setFirstLineIndent(480);
            XWPFRun runContent = content.createRun();
            runContent.setText("Apache POI 是处理 Microsoft Office 文档格式的领先库之一，特别是对于需要在 Java 应用程序中处理 Excel 和 Word 文件的开发者。Apache POI 不仅支持文档的读写操作，还能处理复杂的格式和数据结构，如表格、图表、图片、超链接等。利用 Apache POI 开发者可以在不需要 Microsoft Office 软件的情况下，创建、修改或读取Excel、Word和其他Office文档。这对于需要在服务器端自动处理 Office 文档的应用程序尤其有价值，例如自动报告生成、数据分析应用和内容管理系统。");
            runContent.setFontFamily("宋体");
            createChapterTitle(document, "第二章 简介", 1);
            createChapterTitle(document, "2.1 什么是Word", 2);
            createChapterTitle(document, "2.2 为什么要用Word", 2);
            createChapterTitle(document, "2.3 如何使用Word", 2);
            createChapterTitle(document, "第三章 使用方法", 1);
            createChapterTitle(document, "3.1 基本操作", 2);
            createChapterTitle(document, "3.2 高级功能", 2);
            createChapterTitle(document, "第四章 总结", 1);
            createChapterTitle(document, "4.1 总结Word的优点", 2);
            createChapterTitle(document, "4.2 总结Word的缺点", 2);
            createChapterTitle(document, "4.3 展望未来", 2);

            // 生成目录
//            document.createTOC();

            // 更新目录
            document.enforceUpdateFields();
            // 保存文档
            WordDocument.saveDocument(document, FILE_PATH + +System.currentTimeMillis() + "CreateWordWithTOC.docx");
        } catch (Exception e) {
            throw new RuntimeException("创建word文档对象失败！", e);
        }
    }

    /**
     * 创建折线图测试
     */
    @Test
    public void createLineChartTest() {
        try (XWPFDocument document = WordDocument.createDocument()) {
            LineChartForm chartForm = LineChartForm.builder()
                    .chartTitle("使用POI创建的折线图示例")
                    .overlay(false)
                    .xAxisTitle("日期（年月）")
                    .yAxisTitle("粉丝数（个）")
                    .legendTitle("粉丝")
                    .xData(new String[]{"2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12"})
                    .yData(new Number[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57})
                    .build();
            createLineChart(document, chartForm);
            WordDocument.saveDocument(document, FILE_PATH + "CreateWordLineChart.docx");
        } catch (Exception e) {
            throw new RuntimeException("创建word文档对象失败！", e);
        }
    }

    /**
     * 创建柱状图测试
     */
    @Test
    public void createBarChartTest() {
        try (XWPFDocument document = WordDocument.createDocument()) {
            BarChartForm chartForm = BarChartForm.builder()
                    .chartTitle("使用POI创建的柱状图示例")
                    .overlay(false)
                    .xAxisTitle("日期（年月）")
                    .yAxisTitle("粉丝数（个）")
                    .legendTitle("粉丝")
                    .xData(new String[]{"2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12"})
                    .yData(new Number[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57})
                    .build();

            createBarChart(document, chartForm);
            WordDocument.saveDocument(document, FILE_PATH + "CreateWordBarChart.docx");
        } catch (Exception e) {
            throw new RuntimeException("创建word文档对象失败！", e);
        }
    }

    /**
     * 创建饼图测试
     */
    @Test
    public void createPieChartTest() {
        try (XWPFDocument document = WordDocument.createDocument()) {
            PieChartForm chartForm = new PieChartForm();
            chartForm.setChartTitle("使用POI创建的饼图示例");
            chartForm.setOverlay(false);
            chartForm.setLegendTitle("粉丝");
            chartForm.setXData(new String[]{"2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12"});
            chartForm.setYData(new Number[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57});
            createPieChart(document, chartForm);
            WordDocument.saveDocument(document, FILE_PATH + "CreateWordPieChart.docx");
        } catch (Exception e) {
            throw new RuntimeException("创建word文档对象失败！", e);
        }
    }
}
